package com.mark.org.controller;

import com.mark.basic.util.AjaxResult;
import com.mark.basic.util.PageList;
import com.mark.org.domain.Employee;
import com.mark.org.query.EmployeeQuery;
import com.mark.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @GetMapping("/{id}")
    /**
     * 查询单个数据
     */
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.listById(id);
    }
    @GetMapping
    /**
     * 查询所有数据
     */
    public List<Employee> queryAll(){
        return employeeService.listAll();
    }
    @DeleteMapping("/{id}")
    /**
     * 删除数据
     */
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }
    }
    @PutMapping
    /**
     * 添加或修改数据
     */
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        System.err.println(employee);
        if (employee.getId() == null) {
            employeeService.add(employee);
        } else {
            employeeService.update(employee);
        }
        return AjaxResult.me();
    }
    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        PageList<Employee> aa = employeeService.queryPage(query);
        return aa;
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            System.err.println(ids);
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }
    }
}